package com.android.happymall.fragment.member;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.happymall.R;
import com.android.happymall.fragment.BaseLazyFragment;
import com.android.happymall.model.detail.SearchLogisticsData;
import com.android.happymall.model.detail.SearchLogisticsDataCallback;
import com.android.happymall.utils.ConstantsUrl;
import com.android.happymall.utils.HttpUtils;
import com.android.happymall.utils.MyLog;
import com.android.happymall.utils.ObjectUtils;
import com.mcxtzhang.nestlistview.NestFullListView;
import com.mcxtzhang.nestlistview.NestFullListViewAdapter;
import com.mcxtzhang.nestlistview.NestFullViewHolder;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 *物流详情页
 */
public class Fragment_Mine_Express extends BaseLazyFragment {


    @BindView(R.id.top)
    View top;
    @BindView(R.id.wuliu_list)
    NestFullListView wuliuList;
    @BindView(R.id.scroll)
    ScrollView scroll;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.wuliu_kuaidiname)
    TextView wuliu_kuaidiname;
    @BindView(R.id.wuliu_kuaidinumber)
    TextView wuliu_kuaidinumber;
    @BindView(R.id.wuliu_kuaidiphone)
    TextView wuliu_kuaidiphone;
    private FragmentActivity mcontext;
    private View view;
    private LayoutInflater inflater;
    private boolean networkConnected;
    private static final String ARG_PARAM1 = "param1";
    private String order_sn;

    public Fragment_Mine_Express() {

    }

    public static Fragment_Mine_Express newInstance(String need_id) {
        Fragment_Mine_Express fragment = new Fragment_Mine_Express();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, need_id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            order_sn = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    protected void initPrepare() {
        initExpress();
    }

    @Override
    protected void onInvisible() {

    }

    @Override
    protected void initData() {
        GetLogisticList();
    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_mine_order_express, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }


    private ArrayList<String> list_express = new ArrayList<>();

    private void initExpress() {
        scroll.smoothScrollTo(0, 0);
        top.setFocusable(true);
        top.setFocusableInTouchMode(true);
        top.requestFocus();

        wuliuList.setAdapter(new NestFullListViewAdapter<String>(R.layout.item_express, list_express) {
            @Override
            public void onBind(int position, String testBean, NestFullViewHolder holder) {
                holder.setText(R.id.item_express_tv, testBean);
            }
        });

    }

    public  void GetLogisticList()
    {
        String address = ConstantsUrl.domain + ConstantsUrl.SEARCHLOGISTICS;
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        HashMap<String,String> map = new HashMap<>();
        map.put("order_sn",order_sn);
        MyLog.e("ordddder_sn",order_sn);
        if (networkConnected) {
            HttpUtils.get(address, map, dataCallback);
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
        }
    }

    SearchLogisticsDataCallback dataCallback = new SearchLogisticsDataCallback() {

        @Override
        public void onError(Call call, Exception e, int id) {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
        }

        @Override
        public void onResponse(SearchLogisticsData response, int id) {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
            if (response.getData().getWuliu() != null) {
                for (int i = 0; i <response.getData().getWuliu().size(); i++) {
                    list_express.add(response.getData().getWuliu().get(i).getTime()+response.getData().getWuliu().get(i).getContext());
                }
                wuliuList.updateUI();
                if(response.getData().getExpress()!=null)
                {
                    wuliu_kuaidiname.setText(response.getData().getExpress());
                }
                if(response.getData().getContact_mobile()!=null)
                {
                    wuliu_kuaidiphone.setText(response.getData().getContact_mobile());
                }
                if(response.getData().getExpress_num()!=null)
                {
                    wuliu_kuaidinumber.setText(response.getData().getExpress_num());
                }
            }
        }
    };

    @OnClick(R.id.click_resetnetwork_refresh)
    public void onClick() {
        ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
        GetLogisticList();
    }
}
